package com.example.cdctest.controller;

import com.example.cdctest.pojo.Answers;
import com.example.cdctest.pojo.Question;
import com.example.cdctest.pojo.RandomTestRecord;
import com.example.cdctest.pojo.UserInfo;
import com.example.cdctest.service.QuestionService;
import org.apache.commons.collections4.QueueUtils;
import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/services")
public class RandomTestController {
    private RandomTestRecord randomTestRecord = new RandomTestRecord();
    @Resource
    private QuestionService questionService;

    @RequestMapping("random_test")
    public Object randomTest(HttpSession session) {
        UserInfo loginUser = (UserInfo) session.getAttribute("loginUser");
        if (loginUser == null) {
            return "redirect:/services/login";
        }
        Object randomTestQuestions = (Object) session.getAttribute("questions");
        if (randomTestQuestions != null) {
            return "services/random_test";
        } else {
            List<Question> randomTestQuestions1 = questionService.randomTest();
            JSONArray randomTestQuestions2 = new JSONArray(randomTestQuestions1);
            int num = 0;
            int achievement = 0;
            session.setAttribute("listQuestions", randomTestQuestions1);
            session.setAttribute("achievement", achievement);
            session.setAttribute("num", num);
            session.setAttribute("questions", randomTestQuestions2);
            return "services/random_test";
        }
    }

    @RequestMapping("downQuestion")
    @ResponseBody
    public List downQuestion(String listSelect, HttpSession session) {
        int num = (int) session.getAttribute("num");
        List<Question> questions = (List<Question>)session.getAttribute("listQuestions");
        System.out.println(questions.get(num).getYesQuestion());
        if (listSelect != "") {
            String[] istSelect=listSelect.split(",");
            List<Answers> answers=questions.get(num).getAnswers();
            boolean yes;
            int z=0;
            int[] trueAnswers=new int[10];
            for (int i = 0; i <answers.size() ; i++) {
                if(answers.get(i).getTrueAnswer()){
                    trueAnswers[z]=answers.get(i).getId();
                    z++;
                }
            }
            int y=0;
            for (int j = 0; j < istSelect.length; j++) {
                for (int k = 0; k < trueAnswers.length; k++) {
                    if(trueAnswers[k]==Integer.parseInt(istSelect[j])){
                        y++;
                    }
                }
            }
            if(z==y){
                yes=true;
                questions.get(num).setTrue(true);
            }else{
                yes=false;
                questions.get(num).setTrue(false);
            }
            if(istSelect.length!=z){
                yes=false;
                questions.get(num).setTrue(false);
            }
                for (int j = 0; j <questions.get(num).getAnswers().size() ; j++) {
                    questions.get(num).getAnswers().get(j).setUserAnswers(false);
                    for (int i = 0; i <istSelect.length ; i++) {
                    if(questions.get(num).getAnswers().get(j).getId()==Integer.parseInt(istSelect[i])){
                        questions.get(num).getAnswers().get(j).setUserAnswers(true);
                    }
                }

            }
            if (yes) {
                System.out.println(questions.get(num).getYesQuestion());
                if(questions.get(num).getYesQuestion()==false||(questions.get(num).getYesQuestion()==true&&questions.get(num).isLastAnswer()==false)){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement += 20);
                }
            }
            if(!yes){
                if(questions.get(num).getYesQuestion()&&questions.get(num).isLastAnswer()==true){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement -= 20);
                }
            }
            questions.get(num).setYesQuestion(true);
            questions.get(num).setLastAnswer(questions.get(num).isTrue());
            num=num+=1;
            if (num==questions.size()){
                num=questions.size()-1;
            }
            session.setAttribute("num",num);
            JSONArray question1=new JSONArray(questions);
            session.setAttribute("questions",question1);
            return questions;

        }
        questions.get(num).setYesQuestion(true);
        questions.get(num).setLastAnswer(questions.get(num).isTrue());
        num=num+=1;
        if (num==questions.size()){
            num=questions.size()-1;
        }
        session.setAttribute("num",num);
        JSONArray question2=new JSONArray(questions);
        return questions;
    }
    @RequestMapping("upQuestion")
    @ResponseBody
    public List upQuestion(String listSelect, HttpSession session) {
        int num = (int) session.getAttribute("num");
        List<Question> questions = (List<Question>)session.getAttribute("listQuestions");
        if (listSelect != "") {
            String[] istSelect=listSelect.split(",");
            List<Answers> answers=questions.get(num).getAnswers();
            boolean yes;
            int z=0;
            int[] trueAnswers=new int[10];
            for (int i = 0; i <answers.size() ; i++) {
                if(answers.get(i).getTrueAnswer()){
                    trueAnswers[z]=answers.get(i).getId();
                    z++;
                }
            }
            int y=0;
            for (int j = 0; j < istSelect.length; j++) {
                for (int k = 0; k < trueAnswers.length; k++) {
                    if(trueAnswers[k]==Integer.parseInt(istSelect[j])){
                        y++;
                    }
                }
            }
            if(z==y){
                yes=true;
                questions.get(num).setTrue(true);
            }else{
                yes=false;
                questions.get(num).setTrue(false);

            }
            if(istSelect.length!=z){
                yes=false;
                questions.get(num).setTrue(false);
            }
                for (int j = 0; j <questions.get(num).getAnswers().size() ; j++) {
                    questions.get(num).getAnswers().get(j).setUserAnswers(false);
                    for (int i = 0; i <istSelect.length ; i++) {
                    if(questions.get(num).getAnswers().get(j).getId()==Integer.parseInt(istSelect[i])){
                        questions.get(num).getAnswers().get(j).setUserAnswers(true);
                    }
                }

            }
            if (yes) {
                if(questions.get(num).getYesQuestion()==false||(questions.get(num).getYesQuestion()==true&&questions.get(num).isLastAnswer()==false)){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement += 20);
                }
            }
            if(!yes){
                if(questions.get(num).getYesQuestion()&&questions.get(num).isLastAnswer()==true){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement -= 20);
                }
            }
            questions.get(num).setYesQuestion(true);
            questions.get(num).setLastAnswer(questions.get(num).isTrue());
            num=num-=1;
            if (num<=0){
                num=0;
            }
            session.setAttribute("num",num);
            JSONArray question1=new JSONArray(questions);
            session.setAttribute("questions",question1);
            return questions;

        }
        questions.get(num).setYesQuestion(true);
        questions.get(num).setLastAnswer(questions.get(num).isTrue());
        num=num-=1;
        if (num<=0){
            num=0;
        }
        session.setAttribute("num",num);
        return questions;
    }
    @RequestMapping("numQuestion")
    @ResponseBody
    public List numQuestion(String listSelect, HttpSession session,String numm) {
        int num = (int) session.getAttribute("num");
        List<Question> questions = (List<Question>)session.getAttribute("listQuestions");
        if (listSelect != "") {
            String[] istSelect=listSelect.split(",");
            List<Answers> answers=questions.get(num).getAnswers();
            boolean yes;
            int z=0;
            int[] trueAnswers=new int[10];
            for (int i = 0; i <answers.size() ; i++) {
                if(answers.get(i).getTrueAnswer()){
                    trueAnswers[z]=answers.get(i).getId();
                    z++;
                }
            }
            int y=0;
            for (int j = 0; j < istSelect.length; j++) {
                for (int k = 0; k < trueAnswers.length; k++) {
                    if(trueAnswers[k]==Integer.parseInt(istSelect[j])){
                        y++;
                    }
                }
            }
            if(z==y){
                yes=true;
                questions.get(num).setTrue(true);
            }else{
                yes=false;
                questions.get(num).setTrue(false);
            }
            if(istSelect.length!=z){
                yes=false;
                questions.get(num).setTrue(false);
            }
                for (int j = 0; j <questions.get(num).getAnswers().size() ; j++) {
                    questions.get(num).getAnswers().get(j).setUserAnswers(false);
                    for (int i = 0; i <istSelect.length ; i++) {
                        if(questions.get(num).getAnswers().get(j).getId()==Integer.parseInt(istSelect[i])){
                        questions.get(num).getAnswers().get(j).setUserAnswers(true);
                    }
                }

            }
            if (yes) {
                if(questions.get(num).getYesQuestion()==false||(questions.get(num).getYesQuestion()==true&&questions.get(num).isLastAnswer()==false)){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement += 20);
                }
            }
            if(!yes){
                if(questions.get(num).getYesQuestion()&&questions.get(num).isLastAnswer()==true){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement -= 20);
                }
            }
            questions.get(num).setYesQuestion(true);
            questions.get(num).setLastAnswer(questions.get(num).isTrue());
            num=Integer.parseInt(numm)-1;
            if (num<=0){
                num=0;
            }
            if (num==questions.size()){
                num=questions.size()-1;
            }
            session.setAttribute("num",num);
            JSONArray question1=new JSONArray(questions);
            session.setAttribute("questions",question1);
            return questions;

        }
        questions.get(num).setYesQuestion(true);
        questions.get(num).setLastAnswer(questions.get(num).isTrue());
        num=Integer.parseInt(numm)-1;
        if (num<=0){
            num=0;
        }
        if (num==questions.size()){
            num=questions.size()-1;
        }
        session.setAttribute("num",num);
        return questions;
    }
    @RequestMapping("randomTestEnd")
    public ModelAndView randomTestEnd(String listSelect, HttpSession session){
        int num = (int) session.getAttribute("num");
        List<Question> questions = (List<Question>)session.getAttribute("listQuestions");
        if (listSelect != "") {
            String[] istSelect=listSelect.split(",");
            List<Answers> answers=questions.get(num).getAnswers();
            boolean yes;
            int z=0;
            int[] trueAnswers=new int[10];
            for (int i = 0; i <answers.size() ; i++) {
                if(answers.get(i).getTrueAnswer()){
                    trueAnswers[z]=answers.get(i).getId();
                    z++;
                }
            }
            int y=0;
            for (int j = 0; j < istSelect.length; j++) {
                for (int k = 0; k < trueAnswers.length; k++) {
                    if(trueAnswers[k]==Integer.parseInt(istSelect[j])){
                        y++;
                    }
                }
            }
            if(z==y){
                yes=true;
                questions.get(num).setTrue(true);
            }else{
                yes=false;
                questions.get(num).setTrue(false);
            }
            if(istSelect.length!=z){
                yes=false;
                questions.get(num).setTrue(false);
            }
            for (int j = 0; j <questions.get(num).getAnswers().size() ; j++) {
                questions.get(num).getAnswers().get(j).setUserAnswers(false);
                for (int i = 0; i <istSelect.length ; i++) {
                    if(questions.get(num).getAnswers().get(j).getId()==Integer.parseInt(istSelect[i])){
                        questions.get(num).getAnswers().get(j).setUserAnswers(true);
                    }
                }

            }
            if (yes) {
                if(questions.get(num).getYesQuestion()==false||(questions.get(num).getYesQuestion()==true&&questions.get(num).isLastAnswer()==false)){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement += 20);
                }
            }
            if(!yes){
                if(questions.get(num).getYesQuestion()&&questions.get(num).isLastAnswer()==true){
                    int achievement = (int) session.getAttribute("achievement");
                    session.setAttribute("achievement", achievement -= 20);
                }
            }
            questions.get(num).setYesQuestion(true);
            questions.get(num).setLastAnswer(questions.get(num).isTrue());
            int achievement2=(int)session.getAttribute("achievement");
            System.out.println(achievement2);
            session.removeAttribute("achievement");
            session.removeAttribute("num");
            session.removeAttribute("listQuestions");
            session.removeAttribute("questions");
            ModelAndView modelAndView=new ModelAndView();
            modelAndView.addObject("achievement",achievement2);
            modelAndView.setViewName("/services/end");
            return modelAndView;

        }
        questions.get(num).setYesQuestion(true);
        questions.get(num).setLastAnswer(questions.get(num).isTrue());
        int achievement1=(int)session.getAttribute("achievement");
        System.out.println(achievement1);
        session.removeAttribute("achievement");
        session.removeAttribute("num");
        session.removeAttribute("listQuestions");
        session.removeAttribute("questions");
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("achievement",achievement1);
        modelAndView.setViewName("/services/end");
        return modelAndView;
    }
}
